package com.jy.adscm.operation.hr.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.hr.bean.HrResumeAttachment;
import com.jy.adscm.operation.hr.service.HrResumeAttachmentService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.utils.AdscmSystemUtils;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2020-01-07
 */
@RestController
@RequestMapping("/adscm/operation/hrResumeAttachment")
public class HrResumeAttachmentController {
    @Value("${zltx.hr.upload}")
    private  String annexUploadPath;//附件上传路径
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    HrResumeAttachmentService hrResumeAttachmentService;
    @RequestMapping("upload")
    public SimpleCoreResponse uploadHrResumeAttachment(HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        String hrResumeAttachmentstr = request.getParameter("hrResumeAttachment");
        if (hrResumeAttachmentstr==null||"".equals(hrResumeAttachmentstr)){
            throw  new AdscmSystemException("qsAttachmentstr 没得");
        }
        ObjectMapper mapper = new ObjectMapper();
        HrResumeAttachment hrResumeAttachment = mapper.readValue(hrResumeAttachmentstr, HrResumeAttachment.class);
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<String> paths = null;
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        if(multipartResolver.isMultipart(request)){
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;

            String path = annexUploadPath /*+ File.separator*/ + UUID.randomUUID().toString().replaceAll("-","") + File.separator;

            paths = AdscmSystemUtils.getInstance().uploadFileNoNewName(multiRequest,path);
        }
        if (paths!=null){
            String path = paths.get(0);
            hrResumeAttachment.setPath(path);
            String  fileName =  path.substring(path.lastIndexOf("\\") + 1);
            hrResumeAttachment.setName(fileName);
            hrResumeAttachment.setCreateTime(new Date());
            hrResumeAttachment.setCreator(token.getUser().getId().toString());
        }

        return new SimpleCoreResponse("200","上传成功",hrResumeAttachmentService.insert(hrResumeAttachment));
    }
    @RequestMapping("download/{id}")
    public SimpleCoreResponse downloadHrResumeAttachment(@PathVariable String id, HttpServletRequest request, HttpServletResponse response)throws AdscmSystemException {
        HrResumeAttachment hrResumeAttachment = hrResumeAttachmentService.selectById(id);
        if (hrResumeAttachment==null){
            throw  new AdscmSystemException("没找到文件");
        }
        AdscmSystemUtils.getInstance().downloadFile(hrResumeAttachment.getPath(),response);
        return new SimpleCoreResponse("200","下载成功",null);
    }
    @RequestMapping("selectByRid/{rid}")
    public  SimpleCoreResponse  selectHrResumeAttachments(@PathVariable Integer  rid)throws AdscmSystemException{
        List<HrResumeAttachment> hrResumeAttachments=hrResumeAttachmentService.selectByRid(rid);
        if (hrResumeAttachments==null){
            hrResumeAttachments=new ArrayList<>();
        }
        return  new SimpleCoreResponse("200","查询成功",hrResumeAttachments);
    }
    @RequestMapping("delete/{id}")
    public  SimpleCoreResponse  deleteHrResumeAttachments(@PathVariable Integer id ,HttpServletRequest request)throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result=hrResumeAttachmentService.deleteHrResumeAttachments(id,token);
        return  new SimpleCoreResponse("200","查询成功",result);
    }
}

